/***************
use texture
***************/

#ifndef SIMPLEAPP6_H_
#define SIMPLEAPP6_H_

#include "CEGuiD3D10BaseApplication.h"


D3DXVECTOR4                         g_vMeshColor( 0.7f, 0.7f, 0.7f, 1.0f );

class SimpleApp: public CEGuiD3D10BaseApplication
{
    SubMesh sb;

    D3DXMATRIX                  g_World;
    D3DXMATRIX                  g_View;
    D3DXMATRIX                  g_Projection;
    ID3D10ShaderResourceView*           g_pTextureRV;

public:

    void initMatrix()
    {
        RECT rc;
        GetClientRect( pimpl->d_window, &rc );
        UINT width = rc.right - rc.left;
        UINT height = rc.bottom - rc.top;

        // Initialize the world matrices
        D3DXMatrixIdentity( &g_World );

        // Initialize the view matrix
        D3DXVECTOR3 Eye( 0.0f, 3.0f, -6.0f );
        D3DXVECTOR3 At( 0.0f, 1.0f, 0.0f );
        D3DXVECTOR3 Up( 0.0f, 1.0f, 0.0f );
        D3DXMatrixLookAtLH( &g_View, &Eye, &At, &Up );

        // Initialize the projection matrix
        D3DXMatrixPerspectiveFovLH( &g_Projection, ( float )D3DX_PI * 0.25f, 
            width / ( FLOAT )height, 0.1f, 100.0f );

        bvRenderer->setEffectVariableByName(sb.eftId, "View", EFFECT_VARIABLE_MATRIX,
            (float*)&g_View);
        bvRenderer->setEffectVariableByName(sb.eftId, "Projection", EFFECT_VARIABLE_MATRIX,
            (float*)&g_Projection);
        bvRenderer->setEffectVariableByName(sb.eftId, "txDiffuse", 
            EFFECT_VARIABLE_SHADERRESOURCE, g_pTextureRV);
    }

    HRESULT createScene()
    {
        bvRenderer = new BvD3D10Renderer(pimpl->d_device);
        ZeroMemory(&sb, sizeof(sb));

        sb.eftId = bvRenderer->addEffect("Tutorial07.fx");
        sb.techName = "Render";

        // Set the input layout
        FormatDesc fd[2] =
        {
            {0, TYPE_VERTEX, FORMAT_FLOAT, 3},
            {0, TYPE_TEXCOORD, FORMAT_FLOAT, 2},
        };

        bvRenderer->addMeshVertexFormat(fd, 2, sb);

        // Create vertex buffer
        struct SimpleVertex
        {
            D3DXVECTOR3 Pos;
            D3DXVECTOR2 Tex;
        };

        SimpleVertex vertices[] =
        {
            { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
            { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

            { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
            { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

            { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
            { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
            { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
            { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

            { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
            { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

            { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
            { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

            { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
            { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
            { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
        };

        sb.vbId = bvRenderer->addVertexBuffer(vertices, sizeof(vertices));

        // Create index buffer
        DWORD indices[] =
        {
            3,1,0,
            2,1,3,

            6,4,5,
            7,4,6,

            11,9,8,
            10,9,11,

            14,12,13,
            15,12,14,

            19,17,16,
            18,17,19,

            22,20,21,
            23,20,22
        };

        sb.ibId = bvRenderer->addIndexBuffer(indices, sizeof(indices[0]), 
            sizeof(indices)/sizeof(indices[0]));
        bvRenderer->addSubMesh(sb);

        g_pTextureRV = bvRenderer->createSRVFromFile("../models/seafloor.dds");

        initMatrix();

        return S_OK;
    }

    void drawPerFrame()
    {
        // Update our time
        static float t = 0.0f;
        static DWORD dwTimeStart = 0;
        DWORD dwTimeCur = GetTickCount();
        if( dwTimeStart == 0 )
            dwTimeStart = dwTimeCur;
        t = ( dwTimeCur - dwTimeStart ) / 1000.0f;

        // Rotate cube around the origin
        D3DXMatrixRotationY( &g_World, t );
        

        // Modify the color
        g_vMeshColor.x = ( sinf( t * 1.0f ) + 1.0f ) * 0.5f;
        g_vMeshColor.y = ( cosf( t * 3.0f ) + 1.0f ) * 0.5f;
        g_vMeshColor.z = ( sinf( t * 5.0f ) + 1.0f ) * 0.5f;

        bvRenderer->setEffectVariableByName(sb.eftId, "World", 
            EFFECT_VARIABLE_MATRIX, g_World);
        bvRenderer->setEffectVariableByName(sb.eftId, "vMeshColor", 
            EFFECT_VARIABLE_VECTOR_FLOAT, g_vMeshColor);

        bvRenderer->drawMeshes(PRIM_TRIANGLES, 36);
    }
};

#endif //SIMPLEAPP6_H_
